home *** CD-ROM | disk | FTP | other *** search
/ Amiga Tools 5 / Amiga Tools 5.iso / grafik / converter / limbo4.0 / src / 2d / main.c < prev    next >
Encoding:
C/C++ Source or Header  |  1996-07-16  |  8.9 KB  |  261 lines

  1. #include "includes.h"
  2.  
  3.  int Quiet=FALSE;
  4.  
  5. /**************************************|****************************************
  6. Routine   : main
  7. Input  par: int argc (argument count)
  8.             char *argv[] (pointer to arguments)
  9. Output par: none
  10. Function  : Main program. Inputs arguments from user. Calls Encoder.
  11. ***************************************|***************************************/
  12.  
  13.  void main(int argc,char *argv[])
  14.   {
  15.    char c;
  16.    char *dd,*filename,*cmd;
  17.    int B=4,Delta=8; /* default vals */ 
  18.    int Iterations=6,MaxX=0,MaxY=0,NSquare=0,LLevels=0,OffX=0,OffY=0,snr;
  19.    int MinList=4,MaxList=10,FeatureDimensions=3,GridResolution=1000,i;
  20.     
  21.    LimboHeader *Head=GimmeALimboHeader();
  22.    Parameter   *Par;
  23.    Transformation ***FCCodes;
  24.  
  25.    Par=GimmeAParameter(); /* Initialize vars */
  26.    Par->TMainSub=100;  /* Threshold for main/sub break up */
  27.    Par->TShadeEdge=50; /* Threshold for shade/edge classification */
  28.    Par->TPostProcess=-1; /* Threshold for postprocession */
  29.    Par->XBits=7;  /* X coord bits */
  30.    Par->YBits=7;  /* Y coord bits */
  31.    Par->ZBits=0;  /* Z coord bits */
  32.    Par->SBits=1;  /* mainsub bits - do not change */
  33.    Par->ABits=4;  /* alpha bits */
  34.    Par->AMax=1.0; /* alpha max */
  35.    Par->AMin=0.4; /* alpha min */
  36.    Par->DBits=6;  /* Delta G bits */
  37.    Par->GBits=8;  /* Gray bits - do not change */
  38.    Par->TBits=3;  /* Transformation bits - do not change */
  39.  
  40.    if (argc<3) LearnUsage();
  41.    while(--argc>2)
  42.     {
  43.      c=(*++argv)[0];
  44.      if (!(c=='-')) ErrorHandler(UNKNOWN_OPTION,argv[0]);
  45.      else
  46.       {
  47.        c=(*argv)[1];
  48.        dd= &(*argv)[1];
  49.        switch(c)
  50.         {
  51.          case 'x': MaxX=ReadInteger(&((*argv)[2]));
  52.          break;
  53.          case 'y': MaxY=ReadInteger(&((*argv)[2]));
  54.          break;
  55.          case 'm': MinList=ReadInteger(&((*argv)[2]));
  56.          break;
  57.          case 's': MaxList=ReadInteger(&((*argv)[2]));
  58.          break;
  59.          case 'b': /* Range blocksize */
  60.           {
  61.            B=ReadInteger(&((*argv)[2]));
  62.            if (B<2) B=2;
  63.           }
  64.          break;
  65.          case 'i': Iterations=ReadInteger(&(*argv)[2]); /* Decoder iterations */
  66.          break;
  67.          case 'l': LLevels=ReadInteger(&(*argv)[2]); /* expand levels */
  68.          break;
  69.          case 'n': NSquare=ReadInteger(&(*argv)[2]); /* n-level square */
  70.          break;
  71.          case 'd': Delta=ReadInteger(&((*argv)[2])); /* delta step */
  72.          break;
  73.          case 'f': FeatureDimensions=ReadInteger(&((*argv)[2]));
  74.          break;
  75.          case 'r': GridResolution=ReadInteger(&((*argv)[2]));
  76.          break;
  77.          case 'q': Quiet=TRUE;
  78.          break;
  79.          case 'A': /* alpha range */ 
  80.           {
  81.            if      (!strncmp("Amin",dd,4)) Par->AMin=ReadFloat(&((*argv)[5]));
  82.            else if (!strncmp("Amax",dd,4)) Par->AMax=ReadFloat(&((*argv)[5]));
  83.            else ErrorHandler(UNKNOWN_OPTION,argv[0]);
  84.            if (Par->AMax>2.55) Par->AMax=2.55;
  85.            if (Par->AMin<0.00) Par->AMin=0.00;
  86.            break;
  87.           }
  88.          case 'Q': /* quant */ 
  89.           {
  90.            if      (!strncmp("Qa",dd,2)) Par->ABits=ReadInteger(&((*argv)[3]));
  91.            else if (!strncmp("Qd",dd,2)) Par->DBits=ReadInteger(&((*argv)[3]));
  92.            else ErrorHandler(UNKNOWN_OPTION,argv[0]);
  93.            break;
  94.           }
  95.          case 'o': /* offsets */ 
  96.           {
  97.            if      (!strncmp("ox",dd,2)) OffX=ReadInteger(&((*argv)[3]));  /* offset x  */
  98.            else if (!strncmp("oy",dd,2)) OffY=ReadInteger(&((*argv)[3]));  /* offset y  */
  99.            else ErrorHandler(UNKNOWN_OPTION,argv[0]);
  100.            break;
  101.           }
  102.          case 'T':  /* Threshold parameters */
  103.           {
  104.            /* main/sub thres */
  105.            if      (!strncmp("Tm",dd,2)) Par->TMainSub =ReadInteger(&((*argv)[3]));
  106.            /* shade/edge thres */
  107.            else if (!strncmp("Te",dd,2)) Par->TShadeEdge=ReadInteger(&((*argv)[3]));
  108.            /* post processing */
  109.            else if (!strncmp("Tp",dd,2)) Par->TPostProcess=ReadInteger(&((*argv)[3]));
  110.            else ErrorHandler(UNKNOWN_OPTION,argv[0]);
  111.            break;
  112.           }
  113.          default: ErrorHandler(UNKNOWN_OPTION,argv[0]);
  114.          break;
  115.         }
  116.       }
  117.     }
  118.    
  119.    cmd = &(*++argv)[0];
  120.    filename = &(*++argv)[0];
  121.    
  122.    c=cmd[0];
  123.    switch(c)
  124.     {
  125.      case 'e': /* encode mode */
  126.       {
  127.        int
  128.        StartBlockSize,DeltaSize,Res=pow((float)GridResolution,1.0/(float)FeatureDimensions)+0.5;
  129.        PoolStructure *Pool;
  130.        BitMap *DstImg,*Image;
  131.        clock_t cpu,cpustart;
  132.        
  133.        vprintf(stderr,"Encoding...\n   File:'%s'",filename);
  134.        
  135.        StartBlockSize=B<<NSquare;
  136.        DeltaSize=Delta;
  137.        
  138.        vprintf(stderr,"\nParameters...");
  139.        for (i=NSquare;i>=0;i--) 
  140.        vprintf(stderr,"\n   Level %d:  Domain block size %3d  Range block size %3d  Delta %3d",
  141.                i,B<<(1+i),B<<i,Delta);
  142.        vprintf(stderr,"\n   Threshold for shades/edges:%4d",Par->TShadeEdge);
  143.        vprintf(stderr,"\n   Threshold for main/subs:   %4d",Par->TMainSub);
  144.        vprintf(stderr,"\n   Threshold for postprocess: %4d",Par->TPostProcess);
  145.        vprintf(stderr,"\n   Minimum search list size:  %4d",MinList);
  146.        vprintf(stderr,"\n   Maximum search list size:  %4d",MaxList);
  147.        vprintf(stderr,"\n   Feature space dimensions:  %4d",FeatureDimensions);
  148.        vprintf(stderr,"\n   Feature grid resolution: %6d (%d)",GridResolution,Res);
  149.        
  150.        Image=LoadBitMap(filename,MaxX,MaxY,OffX,OffY,2*StartBlockSize,ORGGFX);
  151.  
  152.        vprintf(stderr,"\ntype:%d",Image->ImgType);
  153.       
  154.        Par->XBits=(int)ceil(log((double)(Image->XSize)/Delta)/log(2.0));
  155.        Par->YBits=(int)ceil(log((double)(Image->YSize)/Delta)/log(2.0));
  156.        vprintf(stderr,"\n   X Bit:%d   Y Bit:%d",Par->XBits,Par->YBits);
  157.        
  158.        cpustart=clock();
  159.        
  160.        vprintf(stderr,"\nGenerating Domain and Range Pool...");
  161.        
  162.        Pool=FindPool(Image,FeatureDimensions,Res,Par->TShadeEdge,NSquare,
  163.                      StartBlockSize,DeltaSize);
  164.        
  165.        cpu=(clock()-cpustart)/CLOCKS_PER_SEC;
  166.        vprintf(stderr,"\n   Pool Generation CPU time used:%d [sec]",cpu);
  167.        
  168.        FCCodes=Encode(Image,NSquare,StartBlockSize,Pool,Par,MinList,MaxList);
  169.        
  170.        cpu=(clock()-cpustart)/CLOCKS_PER_SEC;
  171.        vprintf(stderr,"\nCPU time used:%d [sec]",cpu);
  172.        
  173.        DstImg=Decode(FCCodes,Image->ImgType,Image->XSize,Image->YSize,
  174.                      StartBlockSize,Iterations,0);
  175.  
  176.        vprintf(stderr,"\ntype:%d",DstImg->ImgType);
  177.        
  178.        snr=100*PSNR(Image,DstImg);
  179.        
  180.        Head->HeadSize=(unsigned char)sizeof(LimboHeader)+LIMBOSTRINGSIZE;
  181.        Head->Type=(unsigned char)Image->ImgType;
  182.        Head->MSBx=(unsigned char)(Pool->RangeX*StartBlockSize/256);
  183.        Head->LSBx=(unsigned char)(Pool->RangeX*StartBlockSize-Head->MSBx*256);
  184.        Head->MSBy=(unsigned char)(Pool->RangeY*StartBlockSize/256);
  185.        Head->LSBy=(unsigned char)(Pool->RangeY*StartBlockSize-Head->MSBy*256);
  186.        Head->MSBox=(unsigned char)(OffX/256);
  187.        Head->LSBox=(unsigned char)(OffX-Head->MSBox*256);
  188.        Head->MSBoy=(unsigned char)(OffY/256);
  189.        Head->LSBoy=(unsigned char)(OffY-Head->MSBoy*256);
  190.        
  191.        Head->Col=(unsigned char)8;
  192.        Head->B=(unsigned char)B;
  193.        Head->Delta=(unsigned char)Delta;
  194.        Head->NSquare=(unsigned char)NSquare;
  195.        
  196.        Head->XBits=Par->XBits;
  197.        Head->YBits=Par->YBits;
  198.        
  199.        if (NSquare) Head->SBits=Par->SBits;
  200.        else Head->SBits=0;
  201.        Head->ABits=Par->ABits;
  202.        Head->AMax=(unsigned char)(Par->AMax*100.0); /* max = 2.55 */
  203.        Head->AMin=(unsigned char)(Par->AMin*100.0);
  204.        Head->DBits=Par->DBits;
  205.        Head->GBits=Par->GBits;
  206.        Head->TBits=Par->TBits;
  207.        
  208.        Head->MSBSNR=(unsigned char)(snr/256);
  209.        Head->LSBSNR=(unsigned char)(snr-Head->MSBSNR*256);
  210.        Head->MSBCPU=(unsigned char)(cpu/256);
  211.        Head->LSBCPU=(unsigned char)(cpu-Head->MSBCPU*256);
  212.        
  213.        SaveAndView(filename,DstImg);
  214.        SaveFC(FCCodes,filename,Head);
  215.        vprintf(stderr,"\nDone!\n");
  216.       }
  217.      break;
  218.      
  219.      case 'd':  /* decode mode */
  220.       {
  221.        BitMap *DstImg;
  222.        Head=LoadFC(&FCCodes,filename);
  223.        DstImg=Decode(FCCodes,Head->Type,Head->MSBx*256+Head->LSBx,Head->MSBy*256+Head->LSBy,
  224.                      Head->B<<Head->NSquare,Iterations,LLevels);
  225.        SaveAndView(filename,DstImg);
  226.        vprintf(stderr,"\nDone!\n");
  227.       }
  228.      break;
  229.      
  230.      case 'v':  /* view fractal header */
  231.       {
  232.        Quiet=FALSE;
  233.        Head=LoadFC(&FCCodes,filename);
  234.        HeadInfo(FCCodes,Head);
  235.        vprintf(stderr,"\nDone!\n");
  236.       }
  237.      break;
  238.      
  239.      case 'a':  /* analyze fractal code - produce error image, edg, sha images */
  240.       {
  241.        Quiet=FALSE;
  242.        BitMapAnalyze(filename,Iterations);
  243.        vprintf(stderr,"\nDone!\n");
  244.       }
  245.      break;
  246.      
  247.      case 't':  /* test mode */
  248.       {
  249.        Quiet=FALSE;
  250.        Test(filename);
  251.        vprintf(stderr,"\nDone!\n");
  252.       }
  253.      break;
  254.      
  255.      default : ErrorHandler(UNKNOWN_OPTION,cmd);
  256.      break;
  257.     }
  258.   }
  259.  
  260.  
  261.